accessid = '';
accesskey = '';
host = '';
policyBase64 = '';
signature = '';
callbackbody = '';
filename = '';
key = '';
expire = 0;
g_object_name = '';
g_object_name_type = 'random_name';
now = timestamp = Date.parse(new Date()) / 1000;

function send_request() {
    var xmlhttp = null;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xmlhttp !== null) {
        serverUrl = '/getOssPolicy.do';
        xmlhttp.open("GET", serverUrl, false);
        xmlhttp.send(null);
        return xmlhttp.responseText
    }
    {
        alert("Your browser does not support XMLHTTP.");
    }
}

function get_signature() {
    //可以判断当前expire是否超过了当前时间,如果超过了当前时间,就重新取一下.3s 做为缓冲
    now = timestamp = Date.parse(new Date()) / 1000;
    if (expire < now + 3) {
        body = send_request();
        var obj = eval("(" + body + ")");
        host = obj['host'];
        policyBase64 = obj['policy'];
        accessid = obj['accessid'];
        signature = obj['signature'];
        expire = parseInt(obj['expire']);
        callbackbody = obj['callback'];
        key = obj['dir'];
        return true;
    }
    return false;
}

function random_string(len) {
    len = len || 32;
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var maxPos = chars.length;
    var pwd = '';
    for (var i = 0; i < len; i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

function get_suffix(filename) {
    pos = filename.lastIndexOf('.');
    suffix = '';
    if (pos !== -1) {
        suffix = filename.substring(pos)
    }
    return suffix;
}

function calculate_object_name(filename) {
    if (g_object_name_type === 'local_name') {
        g_object_name += "${filename}"
    } else if (g_object_name_type === 'random_name') {
        suffix = get_suffix(filename);
        g_object_name = key + Date.parse(new Date()) / 1000 + random_string(10) + suffix
    }
    return ''
}

function set_upload_param(up, file, ret) {
    if (ret == false) {
        ret = get_signature();
    }
    g_object_name = key;

    if (file && file !== '') {
        var filename = file.name;
        suffix = get_suffix(filename);
        calculate_object_name(filename);
        var fileData = up.getOption("fileData");
        fileData.push({albumId: $.trim($('.list-navbar.active').attr('data-id')), picName: filename, picUrl: host + "/" + g_object_name, picType: suffix.replace(".", ""), picSize: file.size});
    }

    new_multipart_params = {
        'key': g_object_name,
        'policy': policyBase64,
        'OSSAccessKeyId': accessid,
        'success_action_status': '200', //让服务端返回200,不然，默认会返回204
        'callback': callbackbody,
        'signature': signature
    };

    up.setOption({
        'url': host,
        'multipart_params': new_multipart_params
    });

    up.start();

}

/**
 * 构造 ossUploader
 * @param fileParam
 * @param filesAddedFunc
 * @param uploadCompleteFunc
 * @returns {Uploader|*}
 */
var uploader = new plupload.Uploader({
    runtimes: 'html5,html4',
    browse_button: 'selectfiles',
    multi_selection: true,
    filters: {
        mime_types: [ //只允许上传图片和zip文件
            {title: "Image files", extensions: "jpg,jpeg,gif,png,bmp,webp"}
        ],
        max_file_size: '3mb', //最大只能上传3mb的文件
        prevent_duplicates: true //不允许选取重复文件
    },
    resize: {
        quality: 90,
        preserve_headers: true
    },
    //自定义文件属性值
    fileData: [],
    url: 'http://oss.aliyuncs.com',

    init: {
        PostInit: function () {
        },

        FilesAdded: function (up, files) {
            plupload.each(files, function (file) {
                console.log(file.name);
            });
            set_upload_param(uploader, '', false);
        },

        BeforeUpload: function (up, file) {
            set_upload_param(up, file, true);
        },

        UploadProgress: function (up, file) {
            console.log("上传中" + file.percent + "%...");
        },

        FileUploaded: function (up, file, info) {
            if (info.status === 200) {
                new $.zui.Messager('文件上传成功!', {
                    icon: 'ok-sign',
                    type: 'success',
                    time: 1000
                }).show();
            }
        },

        UploadComplete: function (up) {
            var fileData = JSON.stringify(up.getOption("fileData").reverse());
            $.ajax({
                url: "/admin/picture/savePicture.do",
                type: "post",
                data: {fileData: fileData},
                success: function () {
                    up.setOption("fileData", []);
                    getPictureList(1);
                }
            });
        },

        Error: function (up, err) {
            if (err.code === -600) {
                new $.zui.Messager('请上传小于3M的图片', {
                    icon: 'exclamation-sign',
                    type: 'danger',
                    time: 1000
                }).show();
            } else if (err.code === -601) {
                new $.zui.Messager('只能选择jpg,jpeg,gif,png,bmp,webp格式的图片', {
                    icon: 'exclamation-sign',
                    type: 'danger',
                    time: 1000
                }).show();
            } else if (err.code === -602) {
                new $.zui.Messager('这个文件已经上传过一遍了', {
                    icon: 'exclamation-sign',
                    type: 'danger',
                    time: 1000
                }).show();
            } else {
                new $.zui.Messager(err.response, {
                    icon: 'exclamation-sign',
                    type: 'danger',
                    time: 1000
                }).show();
            }
        }
    }
});
uploader.init();
